package com.sctvcloud.bazhong.ui.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.ui.utils.OnFullClick;
import com.sctvcloud.bazhong.ui.utils.SampleListener;
import com.sctvcloud.bazhong.utils.BitmapUtils;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.listener.INetPromt;
import com.shuyu.gsyvideoplayer.utils.Debuger;
import com.shuyu.gsyvideoplayer.utils.NetworkUtils;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer;
import com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer;

import moe.codeest.enviews.ENDownloadView;
import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by liuyu on 2017/9/17.
 * 继承自ijplayer的直播、视频播放器
 */

public class BasePlayerView extends StandardGSYVideoPlayer {

    protected ImageView centerPlayView;
    protected CustomFontTextView toastView;
    protected CustomFontTextView fullBtn;
    protected ImageView bottomPlay;
    protected CustomFontTextView liveMode;
    protected ViewGroup progressLay;
    protected CheckBox muteToggle;
    protected View vCutButton;
    protected View vCutPreview;
    protected ImageView ivCutView;
    protected View vCutSave;
    protected RelativeLayout fullre;
    protected boolean isLiving;
    protected OnSendComment sendListener;

    protected boolean isMute;//是否静音

    protected boolean isLandscape;

    protected boolean isShowTopPortait = true;

    protected boolean isFullPlaying;

    protected boolean isShowLiveMode;

    protected ImageView thumbImge;

    protected INetPromt netPromt;

    protected OnFullClick fullClick;

    private IPlayerTimeListener timeListener;

    public static int STATUS_PREPARE = 100;
    public static int STATUS_PAUSE = 101;
    public static int STATUS_COMPLETE = 102;
    private int Status;

    protected CompoundButton.OnCheckedChangeListener muteChangeListener;
    protected OnPlayerStatusChanged playerStatusListener;
    protected OnClickListener internalFullClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            onInternalFullClick();
            onClickFullBtn();
        }
    };

    protected IMediaPlayer.OnInfoListener infoListener;
    protected IMediaPlayer.OnCompletionListener completionListener;
    protected IMediaPlayer.OnPreparedListener preparedListener;

    public void setOninfoListener(IMediaPlayer.OnInfoListener infoListener) {
        this.infoListener = infoListener;
    }

    public void setOnCompletionListener(IMediaPlayer.OnCompletionListener completionListener) {
        this.completionListener = completionListener;
    }

    public void setOnPreparedListener(IMediaPlayer.OnPreparedListener preparedListener) {
        this.preparedListener = preparedListener;
    }

    protected SampleListener fullListener = new SampleListener() {
        @Override
        public void onEnterFullscreen(String url, Object... objects) {
            super.onEnterFullscreen(url, objects);
            if (mStandardVideoAllCallBack != null) {
                mStandardVideoAllCallBack.onEnterFullscreen(url, objects);
            }
        }

        @Override
        public void onClickStopFullscreen(String url, Object... objects) {
            super.onClickStopFullscreen(url, objects);
            if (mStandardVideoAllCallBack != null) {
                mStandardVideoAllCallBack.onClickStopFullscreen(url, objects);
            }
        }

        @Override
        public void onClickResumeFullscreen(String url, Object... objects) {
            super.onClickResumeFullscreen(url, objects);
            if (mStandardVideoAllCallBack != null) {
                mStandardVideoAllCallBack.onClickResumeFullscreen(url, objects);
            }
        }

        @Override
        public void onClickBlankFullscreen(String url, Object... objects) {
            super.onClickBlankFullscreen(url, objects);
            if (mStandardVideoAllCallBack != null) {
                mStandardVideoAllCallBack.onClickBlankFullscreen(url, objects);
            }
        }
    };

    public BasePlayerView(Context context, Boolean fullFlag) {
        super(context, fullFlag);
        basePlayerInit(context);
    }

    public BasePlayerView(Context context) {
        super(context);
        basePlayerInit(context);
    }

    public BasePlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        basePlayerInit(context);
    }

    protected void basePlayerInit(Context context) {
//        setStandardVideoAllCallBack(listener);
        mIsTouchWiget = false;
    }

    @Override
    public void setThumbImageView(View view) {
        super.setThumbImageView(view);
    }

    @Override
    protected void init(Context context) {
        mRotateViewAuto = false;
        super.init(context);
        centerPlayView = (ImageView) findViewById(R.id.player_toast_icon);
        bottomPlay = (ImageView) findViewById(R.id.player_bottom_play);
        thumbImge = (ImageView) findViewById(R.id.player_thumb_view);
        mThumbImageView = thumbImge;
        liveMode = (CustomFontTextView) findViewById(R.id.player_top_live_mode);
        toastView = (CustomFontTextView) findViewById(R.id.player_toast_text);
        fullBtn = (CustomFontTextView) findViewById(R.id.player_bottom_full);
        fullre = (RelativeLayout) findViewById(R.id.re_full);
        progressLay = (ViewGroup) findViewById(R.id.player_bottom_lay_progress);
        muteToggle = (CheckBox) findViewById(R.id.player_top_mute);
        vCut = findViewById(R.id.rl_cut);
        vCutButton = findViewById(R.id.v_screen_cut);
        vCutButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = cutScreenBitmap();
                if (vCutPreview != null && ivCutView != null) {
                    vCutPreview.setVisibility(VISIBLE);
                    ivCutView.setImageBitmap(bitmap);
                    ivCutView.setTag(bitmap);
                }
            }
        });
        vCutPreview = findViewById(R.id.ll_cut_preview);
        ivCutView = (ImageView) findViewById(R.id.iv_cut_preview);
        vCutPreview.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ivCutView != null) {
                    boolean isSaved = BitmapUtils.saveImageToGallery(getContext(), (Bitmap) ivCutView.getTag());
                    if (isSaved) {
                        Toast.makeText(getContext(), "保存成功，请到系统相册查看", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), "保存失败", Toast.LENGTH_SHORT).show();
                    }
                    vCutPreview.setVisibility(GONE);
                }

            }
        });
        muteToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isInVideo()) {
                    GSYVideoManager.instance().setNeedMute(isChecked);
                }
                isMute = isChecked;
                if (muteChangeListener != null) {
                    muteChangeListener.onCheckedChanged(buttonView, isChecked);
                }
            }
        });
        if (isMute) {
            muteToggle.setChecked(true);
        }

        if (mFullscreenButton != null) {
            mFullscreenButton.setOnTouchListener(null);
        }
        bottomPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                clickStartIcon();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.video_layout_base;
    }

    @Override
    public void onPrepared() {
        super.onPrepared();

        syncMute();
    }

    @Override
    public void onPrepared(IMediaPlayer player) {
        if (preparedListener != null) {
            preparedListener.onPrepared(player);
        }
    }

    @Override
    protected void hideAllWidget() {
//        JLog.e("GSY  will hide all widget");
        super.hideAllWidget();
        if (mCurrentState == CURRENT_STATE_PAUSE) {
            mStartButton.setVisibility(VISIBLE);
        }
    }

    @Override
    public void onVideoPause() {
        super.onVideoPause();
    }

    @Override
    protected void changeUiToNormal() {
        Debuger.printfLog("changeUiToNormal");
        syncMute();
        updateStartImage();
        setViewShowState(mTopContainer, showTop() ? VISIBLE : INVISIBLE);
        setViewShowState(liveMode, isShowLiveMode ? VISIBLE : INVISIBLE);
        setViewShowState(mBottomContainer, VISIBLE);
        setViewShowState(mStartButton, VISIBLE);
        setViewShowState(mLoadingProgressBar, INVISIBLE);
        setViewShowState(mThumbImageViewLayout, VISIBLE);
        setViewShowState(mBottomProgressBar, INVISIBLE);
        setViewShowState(mBackButton, GONE);
        setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
        setViewShowState(fullBtn, TextUtils.isEmpty(mOriginUrl) ? GONE : VISIBLE);
        if (isRadio) {
            setViewShowState(fullBtn, GONE);
        } else {
            setViewShowState(fullBtn, VISIBLE);
        }
        if (isLiving) {
            setViewShowState(progressLay, GONE);
//            setViewShowState(bottomTitle, VISIBLE);
        } else {
            setViewShowState(progressLay, VISIBLE);
//            setViewShowState(bottomTitle, GONE);
        }

        if (mLoadingProgressBar instanceof ENDownloadView) {
            ((ENDownloadView) mLoadingProgressBar).reset();
        }
    }

    @Override
    protected void changeUiToPreparingShow() {
        Debuger.printfLog("changeUiToPreparingShow");

        setViewShowState(mTopContainer, showTop() ? VISIBLE : INVISIBLE);
        setViewShowState(liveMode, isShowLiveMode ? VISIBLE : INVISIBLE);
        setViewShowState(mBottomContainer, VISIBLE);
        setViewShowState(mStartButton, VISIBLE);
        setViewShowState(mLoadingProgressBar, VISIBLE);
        if (isRadio) {
            setViewShowState(mThumbImageView, VISIBLE);
            mThumbImageViewLayout.setVisibility(VISIBLE);
            setViewShowState(fullBtn, GONE);
        } else {
            setViewShowState(fullBtn, VISIBLE);
            setViewShowState(mThumbImageViewLayout, VISIBLE);
        }
//        setViewShowState(mThumbImageViewLayout, INVISIBLE);
        setViewShowState(mBottomProgressBar, INVISIBLE);
        setViewShowState(mBackButton, GONE);
        setViewShowState(mLockScreen, GONE);

        if (isLiving) {
            setViewShowState(progressLay, GONE);
//            setViewShowState(bottomTitle, VISIBLE);
        } else {
            setViewShowState(progressLay, VISIBLE);
//            setViewShowState(bottomTitle, GONE);
        }

        if (mLoadingProgressBar instanceof ENDownloadView) {
            ENDownloadView enDownloadView = (ENDownloadView) mLoadingProgressBar;
            if (enDownloadView.getCurrentState() == ENDownloadView.STATE_PRE) {
                ((ENDownloadView) mLoadingProgressBar).start();
            }
        }
    }

    @Override
    protected void changeUiToPlayingShow() {
        Debuger.printfLog("changeUiToPlayingShow");

        setViewShowState(mTopContainer, showTop() ? VISIBLE : INVISIBLE);
        setViewShowState(liveMode, isShowLiveMode ? VISIBLE : INVISIBLE);
        setViewShowState(mBottomContainer, VISIBLE);
        if (isStartClick ) {
            setViewShowState(mStartButton, GONE);
        } else {
            setViewShowState(mStartButton, VISIBLE);
        }
        isStartClick = false;
        setViewShowState(mLoadingProgressBar, INVISIBLE);
        if (isRadio) {
            setViewShowState(mThumbImageView, VISIBLE);
            mThumbImageViewLayout.setVisibility(VISIBLE);
            setViewShowState(fullBtn, INVISIBLE);
        } else {
            setViewShowState(mThumbImageViewLayout, INVISIBLE);
            setViewShowState(fullBtn, VISIBLE);
        }
        setViewShowState(mBottomProgressBar, INVISIBLE);
        setViewShowState(mBackButton, GONE);
        setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
//        setViewShowState(fullBtn, VISIBLE);
        if (isLiving) {
            setViewShowState(progressLay, GONE);
//            setViewShowState(bottomTitle, VISIBLE);
        } else {
            setViewShowState(progressLay, VISIBLE);
//            setViewShowState(bottomTitle, GONE);
        }

        if (mLoadingProgressBar instanceof ENDownloadView) {
            ((ENDownloadView) mLoadingProgressBar).reset();
        }
        updateStartImage();
    }

    @Override
    protected void changeUiToPauseShow() {
        Debuger.printfLog("changeUiToPauseShow");

        setViewShowState(mTopContainer, VISIBLE);
        setViewShowState(mBottomContainer, VISIBLE);
        setViewShowState(mStartButton, VISIBLE);
        setViewShowState(mLoadingProgressBar, INVISIBLE);
        if (isRadio) {
            setViewShowState(mThumbImageView, VISIBLE);
            mThumbImageViewLayout.setVisibility(VISIBLE);
            setViewShowState(fullBtn, INVISIBLE);
        } else {
            setViewShowState(mThumbImageViewLayout, INVISIBLE);
            setViewShowState(fullBtn, VISIBLE);
        }
        setViewShowState(mBottomProgressBar, INVISIBLE);
        setViewShowState(mBackButton, GONE);
        setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);

        if (mLoadingProgressBar instanceof ENDownloadView) {
            ((ENDownloadView) mLoadingProgressBar).reset();
        }
        updateStartImage();
        updatePauseCover();
    }

    @Override
    protected void changeUiToPauseClear() {
        super.changeUiToPauseClear();
    }

    @Override
    protected void changeUiToPlayingBufferingShow() {
        Debuger.printfLog("changeUiToPlayingBufferingShow");

        JLog.e("qqaq  state=" + mCurrentState + "   " + this);
        setViewShowState(mTopContainer, showTop() ? VISIBLE : INVISIBLE);
        setViewShowState(liveMode, isShowLiveMode ? VISIBLE : INVISIBLE);
        setViewShowState(mBottomContainer, VISIBLE);
        setViewShowState(mStartButton, VISIBLE);
        setViewShowState(mLoadingProgressBar, VISIBLE);
        if (isRadio) {
            setViewShowState(mThumbImageViewLayout, VISIBLE);
            setViewShowState(mThumbImageView, VISIBLE);
            setViewShowState(fullBtn, INVISIBLE);
        } else {
            setViewShowState(mThumbImageViewLayout, INVISIBLE);
            setViewShowState(fullBtn, VISIBLE);
        }
//        setViewShowState(mThumbImageViewLayout, INVISIBLE);
        setViewShowState(mBottomProgressBar, INVISIBLE);
        setViewShowState(mLockScreen, GONE);
        setViewShowState(mBackButton, GONE);

        if (mLoadingProgressBar instanceof ENDownloadView) {
            ENDownloadView enDownloadView = (ENDownloadView) mLoadingProgressBar;
            if (enDownloadView.getCurrentState() == ENDownloadView.STATE_PRE) {
                ((ENDownloadView) mLoadingProgressBar).start();
            }
        }
    }

    @Override
    protected void changeUiToCompleteShow() {
        Debuger.printfLog("changeUiToCompleteShow");

        setViewShowState(mTopContainer, showTop() ? VISIBLE : INVISIBLE);
        setViewShowState(liveMode, isShowLiveMode ? VISIBLE : INVISIBLE);
        setViewShowState(mBottomContainer, VISIBLE);
        setViewShowState(mStartButton, VISIBLE);
        setViewShowState(mLoadingProgressBar, INVISIBLE);
        setViewShowState(mThumbImageViewLayout, VISIBLE);
        setViewShowState(mBottomProgressBar, INVISIBLE);
        setViewShowState(mBackButton, GONE);
        setViewShowState(mLockScreen, (mIfCurrentIsFullscreen && mNeedLockFull) ? VISIBLE : GONE);
        if (isRadio) {
            setViewShowState(fullBtn, GONE);
        } else {
            setViewShowState(fullBtn, VISIBLE);
        }
        if (mLoadingProgressBar instanceof ENDownloadView) {
            ((ENDownloadView) mLoadingProgressBar).reset();
        }
        updateStartImage();
    }

    @Override
    protected void changeUiToClear() {
        super.changeUiToClear();
        if (mCurrentState == CURRENT_STATE_PAUSE) {
            mStartButton.setVisibility(VISIBLE);
        }
    }

    @Override
    protected void updatePauseCover() {
        super.updatePauseCover();
        isFullPlaying = false;
//        JLog.e("rrsr   update pause");
    }

    @Override
    public GSYBaseVideoPlayer startWindowFullscreen(Context context, boolean actionBar, boolean statusBar) {
        BasePlayerView play = (BasePlayerView) super.startWindowFullscreen(context, actionBar, statusBar);
        play.setLiving(isLiving);
        play.setSendListener(sendListener);
        play.setLandscape(true);
        play.setMute(isMute);
        play.setFullClick(this.fullClick);
//        setViewShowState(pl, GONE);
//        play.fullBtn.setText(R.string.back);
        play.fullBtn.setBackground(getResources().getDrawable(R.drawable.video_shrink));
        play.setViewShowState(play.getBackButton(), GONE);
        if (isLiving) {
            play.setViewShowState(play.getProgressLay(), GONE);
        }
        play.setStandardVideoAllCallBack(fullListener);
        if (mRotateViewAuto) {
            play.setRotateViewAuto(true);
        }
//        play.changeUiToPlayingShow();
        return play;
    }

    @Override
    protected boolean canChangePositionInTouch() {
        return !isLiving;
    }


    /**
     * 显示wifi确定框，如需要自定义继承重写即可
     */
    @Override
    protected void showWifiDialog() {
        if (!NetworkUtils.isAvailable(mContext)) {
            Toast.makeText(mContext, getResources().getString(com.ruihang.ijkplaylib.R.string.no_net), Toast.LENGTH_LONG).show();
            return;
        }
        if (netPromt != null) {
            netPromt.onNetPromt();
            return;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivityContext());
        builder.setMessage(getResources().getString(com.ruihang.ijkplaylib.R.string.tips_not_wifi));
        builder.setPositiveButton(getResources().getString(com.ruihang.ijkplaylib.R.string.tips_not_wifi_confirm), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                startPlayLogic();
            }
        });
        builder.setNegativeButton(getResources().getString(com.ruihang.ijkplaylib.R.string.tips_not_wifi_cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    @Override
    public void onBackFullscreen() {
        super.onBackFullscreen();
    }

    @Override
    public void setTitleStr(String str) {
        super.setTitleStr(str);
        JLog.e("ssas  title=" + str + "    " + mTitleTextView);
//        if (bottomTitle != null) {
//            bottomTitle.setText(str);
//        }
    }

    @Override
    public void release() {
        super.release();
        timeListener = null;
    }

    @Override
    protected void setCurrentTimeStr(CharSequence str) {
        super.setCurrentTimeStr(str);
        if (timeListener != null) {
            timeListener.setCurrentTimeStr(str);
        }
    }

    public void hidePlayIcon(){
        setViewShowState(centerPlayView, GONE);
    }
    /**
     * 定义开始按键显示
     */
    @Override
    protected void updateStartImage() {
        setViewShowState(centerPlayView, VISIBLE);
        switch (mCurrentState) {
            case CURRENT_STATE_PLAYING:
                if (null != playerStatusListener) {
                    playerStatusListener.OnPlaying();
                }
                centerPlayView.setImageResource(R.mipmap.icon_onair);
                bottomPlay.setImageResource(R.mipmap.icon_living_cr_pause_white);
                toastView.setVisibility(GONE);
                break;
            case CURRENT_STATE_ERROR:
                if (null != playerStatusListener) {
                    playerStatusListener.OnError();
                }
                toastView.setVisibility(VISIBLE);
                toastView.setText(R.string.small_problem);
                centerPlayView.setImageResource(R.mipmap.icon_play);
                bottomPlay.setImageResource(R.mipmap.icon_living_cr_play_white);
                break;
            case CURRENT_STATE_AUTO_COMPLETE:
                if (null != playerStatusListener) {
                    playerStatusListener.OnEnd();
                }
                toastView.setVisibility(VISIBLE);
                toastView.setText(R.string.play_complet);
                centerPlayView.setImageResource(R.mipmap.icon_play);
                bottomPlay.setImageResource(R.mipmap.icon_living_cr_play_white);
                break;
            default:
                if (null != playerStatusListener) {
                    playerStatusListener.OnStop();
                }
                toastView.setVisibility(GONE);
                centerPlayView.setImageResource(R.mipmap.icon_play);
                bottomPlay.setImageResource(R.mipmap.icon_living_cr_play_white);
        }
//        isStartClick = false;
    }

    @Override
    public void onInfo(IMediaPlayer mp, int what, int extra) {
        if (infoListener != null) {
            infoListener.onInfo(mp, what, extra);
        }
    }

    @Override
    public void onInfo(int what, int extra) {
        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
            mBackUpPlayingBufferState = mCurrentState;
            //避免在onPrepared之前就进入了buffering，导致一只loading
            if (mHadPlay && mCurrentState != CURRENT_STATE_PREPAREING && mCurrentState > 0)
                setStateAndUi(CURRENT_STATE_PLAYING_BUFFERING_START);

        } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
            if (mBackUpPlayingBufferState != -1) {

                if (mHadPlay && mCurrentState != CURRENT_STATE_PREPAREING && mCurrentState > 0)
                    setStateAndUi(mBackUpPlayingBufferState);

                mBackUpPlayingBufferState = -1;
            }
        } else if (what == IMediaPlayer.MEDIA_INFO_VIDEO_ROTATION_CHANGED) {
            mRotate = extra;
            if (mTextureView != null)
                mTextureView.setRotation(mRotate);
        }
        JLog.e("what=" + what + "   extra=" + extra);

    }

    @Override
    public void onError(int what, int extra) {
        super.onError(what, extra);
        JLog.e("what=" + what + "   extra=" + extra);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    @Override
    protected void resolveNormalVideoShow(View oldF, ViewGroup vp, GSYVideoPlayer gsyVideoPlayer) {
        super.resolveNormalVideoShow(oldF, vp, gsyVideoPlayer);
        if (gsyVideoPlayer == null) {
            return;
        }
        boolean mute = ((BasePlayerView) gsyVideoPlayer).isMute;
        isFullPlaying = gsyVideoPlayer.getCurrentState() == CURRENT_STATE_PLAYING;
        if (this.isMute != mute) {
            setMute(mute);
        }
//        gsyVideoPlayer.geto
//        JLog.e("rrsr  " + gsyVideoPlayer+"      "+gsyVideoPlayer.getCurrentState());
    }

    protected boolean showTop() {
        return isLandscape || isShowTopPortait;
    }

    public void setLiving(boolean living) {
        isLiving = living;
        if (mStandardVideoAllCallBack != null && mStandardVideoAllCallBack instanceof SampleListener) {
            ((SampleListener) mStandardVideoAllCallBack).setLiving(living);
        }
        fullListener.setLiving(living);
    }

    public void setRadio(boolean isRadio) {
        this.isRadio = isRadio;
    }

    public void setLandscape(boolean landscape) {
        isLandscape = landscape;
    }

    public void setShowTopPortait(boolean showTopPortait) {
        isShowTopPortait = showTopPortait;
    }

    protected View getProgressBar() {
        return mProgressBar;
    }

    public ViewGroup getProgressLay() {
        return progressLay;
    }

    public void setShowLiveMode(boolean showLiveMode) {
        isShowLiveMode = showLiveMode;
        JLog.e("is show live mode=" + isShowLiveMode + "   live " + liveMode);
        if (liveMode != null) {
            setViewShowState(liveMode, isShowLiveMode ? VISIBLE : INVISIBLE);
        }
    }

    public void setMute(boolean mute) {
        isMute = mute;
        if (muteToggle != null) {
            muteToggle.setChecked(mute);
            syncMute();
        }
    }

    public boolean isMute() {
        return isMute;
    }

    private void syncMute() {
        if (isMute) {
            if (muteToggle.isChecked()) {
                GSYVideoManager.instance().setNeedMute(true);
            } else {
                muteToggle.setChecked(true);
            }
        } else {
            if (!muteToggle.isChecked()) {
                GSYVideoManager.instance().setNeedMute(false);
            } else {
                muteToggle.setChecked(false);
            }
        }
    }

    /**
     * 是否处于播放流程
     *
     * @return
     */
    protected boolean isInVideo() {
        return mCurrentState >= CURRENT_STATE_PREPAREING && mCurrentState < CURRENT_STATE_AUTO_COMPLETE;
    }

    protected void onInternalFullClick() {
        if (fullClick != null) {
            fullClick.onFullClick(this, isLandscape);

        }
    }

    public void setFullClick(OnFullClick fullClick) {
        this.fullClick = fullClick;
        if (this.fullClick != null) {
            //     fullBtn.setOnClickListener(internalFullClick);
            fullre.setOnClickListener(internalFullClick);
        }
    }

    public void setNetPromt(INetPromt netPromt) {
        this.netPromt = netPromt;
    }

    public void startPlay() {
        if (mStartButton != null) {
            mStartButton.callOnClick();
        } else {
            startPlayLogic();
        }
    }

    public boolean isFullPlaying() {
        GSYBaseVideoPlayer player = getFullWindowPlayer();
//        JLog.e("rrsr           "+player);
        return (player != null && player.getCurrentState() == CURRENT_STATE_PLAYING) || isFullPlaying;
    }

    public void setMuteChangeListener(CompoundButton.OnCheckedChangeListener muteChangeListener) {
        this.muteChangeListener = muteChangeListener;
    }

    public void setTimeListener(IPlayerTimeListener timeListener) {
        this.timeListener = timeListener;
    }

    public void setOrientationChangeEnable(boolean b) {
        if (mOrientationUtils != null) {
            mOrientationUtils.setEnable(b);
        }
    }

    public ImageView getThumbImge() {
        return thumbImge;
    }

    public OrientationUtils getOrientationUtils() {
        return mOrientationUtils;
    }

    @Override
    public void onCompletion(IMediaPlayer player) {
        super.onCompletion();
        if (completionListener != null) {
            completionListener.onCompletion(player);
        }
    }

    protected void onClickFullBtn() {

    }


    public void setSendListener(OnSendComment sendListener) {
        this.sendListener = sendListener;
    }

    public interface OnSendComment {
        void onSendComment();
    }

    public void setOnPlayerStatusListener(OnPlayerStatusChanged playerStatusListener) {
        this.playerStatusListener = playerStatusListener;
    }

    public interface OnPlayerStatusChanged {
        void OnPlaying();

        void OnError();

        void OnStop();

        void OnEnd();
    }

    public int getStatus() {
        return Status;
    }

    public void setStatus(int status) {
        Status = status;
    }
}
